//
//  SEGBillSelectRoomViewController.m
//  ExcellentHome
//
//  Created by 梁昆 on 2016/12/15.
//  Copyright © 2016年 陈区. All rights reserved.
//
#import "CustomTableViewCell.h"
#import "SEGBillSelectRoomViewController.h"

@interface SEGBillSelectRoomViewController ()

@property (nonatomic, strong) UITableView* roomNumber;
@property (nonatomic, strong) NSArray<SEGProfileHouselistModel*>* houseList;

@end

@implementation SEGBillSelectRoomViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    // 直接使用用户信息里面的数据，不走网络请求
    
    self.houseList = [[SEGProfileManager sharedManager].profile.houseList copy];
    if (!self.houseList || self.houseList.count == 0) {
        [SEGAlertView showTmepMessageWithMessage:@"暂无可用的小区"];
        return;
    }
    
    for (SEGProfileHouselistModel *houseModel in self.houseList) {
        if ([houseModel.houseId isEqualToString:self.houseId]) {
            houseModel.isSelected = YES;
        } else {
            houseModel.isSelected = NO;
        }
    }
    [self.roomNumber reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self showCustomNavigationBackButton];
    self.title = @"切换房间";
    self.view.backgroundColor = kColor_bg;

    _reuseIdentifier_RoomNumberCell = @"RoomNumberCell";

    [self setupUI];
}

- (void)setupUI
{
    [self.view addSubview:self.roomNumber];
    [self.roomNumber mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(self.view);
        make.bottom.left.right.equalTo(self.view);
    }];
}

#pragma mark - dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.houseList.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    CustomTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:_reuseIdentifier_RoomNumberCell];

    if (cell == nil) {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_reuseIdentifier_RoomNumberCell];
    }

    cell.contentLabel.numberOfLines = 0;
    [cell.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.bottom.mas_equalTo(-12);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-30);
    }];
    
    cell.contentLabel.text = self.houseList[indexPath.row].fullHouseName;
    [cell.imageSelect setHidden:!self.houseList[indexPath.row].isSelected];

    return cell;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return UITableViewAutomaticDimension;
}

#pragma mark - delegate     选中勾勾显示或隐藏

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    for (int i = 0; i < self.houseList.count; i++) {

        CustomTableViewCell* cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        if (i == indexPath.row) {
            [cell.imageSelect setHidden:YES];
        }
        else {
            [cell.imageSelect setHidden:NO];
        }
    }

    [self.delegate cell_didSelectedRoom:self.houseList[indexPath.row]];

    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - getter

- (UITableView*)roomNumber
{
    if (!_roomNumber) {
        _roomNumber = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
        _roomNumber.delegate = self;
        _roomNumber.dataSource = self;
        _roomNumber.backgroundColor = kColor_bg;
        _roomNumber.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _roomNumber.separatorStyle = UITableViewCellSeparatorStyleNone;
        _roomNumber.estimatedRowHeight = 44;
    }
    return _roomNumber;
}

@end
